﻿using Ding.HttpHelper;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using NPOI.XWPF.UserModel;
using SelfService.Model.Interface;
using SelfService.Model.Models;
using SelfService.XinXing.Models;
using TT;

namespace SelfService.XinXing.Controllers
{
    /// <summary>
    /// 综合预约接口
    /// </summary>
    public class ClinicControllerBase : IClinicControllerBase
    {

        private readonly ReservationConfig _config;
        public ClinicControllerBase(IOptions<ReservationConfig> options)
        {
            _config = options.Value;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public async Task<ResultDetail<object>> Query(string body)
        {
            try
            {
                if (body.TryJsonParse(out ClinicQueryValidate query))
                {
                    var setting = new JsonSerializerSettings()
                    {
                        ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                    };
                    string postData = JsonConvert.SerializeObject(query, setting);
                    var httpItem = new HttpItem
                    {
                        URL = $"{_config.ReservationHost}/shm/reservation/page?cardNo={query.CardNo}&appointType=1&channel=${_config.ReservationChannel}"
                    };
                    HttpResult result = null;
                    await Task.Run(() => { result = new HttpHelper().GetHtml(httpItem); });
                    if (result.Html.TryJsonParse(out ReservationResult<dynamic> response))
                    {
                        if (response.Code == "1000")
                        {
                            return new ResultDetail<object>
                            {
                                Data = JsonConvert.SerializeObject(response.Message, setting),
                                Request = postData,
                                Response = result.Html
                            };
                        }
                        return new ResultDetail<object>
                        {
                            Code = 1,
                            Message = response.Message.ToString(),
                            Request = postData,
                            Response = result.Html
                        };
                    }
                    return new ResultDetail<object>
                    {
                        Code = 1,
                        Message = "解析返回数据失败",
                        Request = postData,
                        Response = result.Html
                    };
                }
                return new ResultDetail<object>
                {
                    Code = 1,
                    Message = "参数错误",
                    Request = body
                };
            }
            catch (Exception e)
            {
                return new ResultDetail<object>
                {
                    Code = 1,
                    Message = $"查询预约列表异常:{JsonConvert.SerializeObject(e)}",
                    Request = body
                };
            }
        }

        /// <summary>
        /// 签到
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDetail<object>> Sign(string body)
        {
            try
            {
                if (body.TryJsonParse(out ClinicSignValidate query))
                {
                    var setting = new JsonSerializerSettings()
                    {
                        ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                    };
                    query.Channel = _config.ReservationChannel;
                    query.Type = ClinicTypeEnum.预约挂号;
                    string postData = JsonConvert.SerializeObject(query, setting);
                    var httpItem = new HttpItem
                    {
                        URL = $"{_config.ReservationHost}/shm/wait/sign",
                        Method = "POST",
                        ContentType = "application/json",
                        Postdata = postData
                    };
                    HttpResult result = null;
                    await Task.Run(() => { result = new HttpHelper().GetHtml(httpItem); });
                    if (result.Html.TryJsonParse(out ReservationResult<object> response))
                    {
                        if (response.Code == "1000")
                        {
                            return new ResultDetail<object>
                            {
                                Data = response.Message,
                                Request = postData,
                                Response = result.Html
                            };
                        }
                        return new ResultDetail<object>
                        {
                            Code = 1,
                            Message = response.Message.ToString(),
                            Request = postData,
                            Response = result.Html
                        };
                    }
                    return new ResultDetail<object>
                    {
                        Code = 1,
                        Message = "解析返回数据失败",
                        Request = postData,
                        Response = result.Html
                    };
                }
                return new ResultDetail<object>
                {
                    Code = 1,
                    Message = "请求参数错误",
                    Request = body
                };
            }
            catch (Exception e)
            {
                return new ResultDetail<object>
                {
                    Code = 1,
                    Message = $"门诊签到异常:{JsonConvert.SerializeObject(e)}",
                    Request = body
                };
            }
        }

        /// <summary>
        /// 支付并签到
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public Task<ResultDetail<object>> PayAndSign(string body)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 当日挂号接口
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public async Task<ResultDetail<object>> Registered(string body)
        {
            try
            {
                if (body.TryJsonParse(out PayAndRegisterValidate query))
                {
                    var setting = new JsonSerializerSettings()
                    {
                        ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                    };
                    query.Channel = _config.ReservationChannel;
                    string postData = JsonConvert.SerializeObject(query, setting);
                    var httpItem = new HttpItem
                    {
                        URL = $"{_config.ReservationHost}/shm/registration/regPayAndSignToday",
                        Method = "POST",
                        ContentType = "application/json",
                        Postdata = postData
                    };
                    HttpResult result = null;
                    await Task.Run(() => { result = new HttpHelper().GetHtml(httpItem); });
                    if (result.Html.TryJsonParse(out ReservationResult<object> response))
                    {
                        if (response.Code == "1000")
                        {
                            return new ResultDetail<object>
                            {
                                Data = response.Message,
                                Request = postData,
                                Response = result.Html
                            };
                        }
                        return new ResultDetail<object>
                        {
                            Code = 1,
                            Message = response.Message.ToString(),
                            Request = postData,
                            Response = result.Html
                        };
                    }
                    return new ResultDetail<object>
                    {
                        Code = 1,
                        Message = "解析返回数据失败",
                        Request = postData,
                        Response = result.Html
                    };
                }
                return new ResultDetail<object>
                {
                    Code = 1,
                    Message = "请求参数错误",
                    Request = body
                };
            }
            catch (Exception e)
            {
                return new ResultDetail<object>
                {
                    Code = 1,
                    Message = $"现场挂号异常:{JsonConvert.SerializeObject(e)}",
                    Request = body
                };
            }
        }
    }
}
